<?php

class ugovori_display extends ugovori{
	
	var $id;
	var $data;
	
	function ugovori_display($id){
		$this->id = $id;
		$this->db =  new db_functions();
		
		$this->displayUgovor();
	}
	
	function displayUgovor(){
		
		// user rights
		$user = $this->getUserLevel();
		switch (intval($user["rights"]["ugovori"])){
			case 0:
				$this->assign("moderator_start","<!--");
				$this->assign("moderator_end","-->");
			case 1:
				$this->assign("admin_start","<!--");
				$this->assign("admin_end","-->");
		}
		
		$ugovor = $this->db->getRow("SELECT * FROM ugovori WHERE id=$this->id LIMIT 1",true);
		if ($ugovor === false) {
			die("Nema ugovora ID:$this->id");
		}

		if (count($ugovor)==0) {
			die("Ugovor $this->id ne postoji.");
		}
		
		$next_ugovor = $this->db->getOne("SELECT id FROM ugovori WHERE id>$this->id ORDER BY id ASC LIMIT 1");
		$prev_ugovor = $this->db->getOne("SELECT id FROM ugovori WHERE id<$this->id ORDER BY id DESC LIMIT 1");
		$last_ugovor = $this->db->getOne("SELECT id FROM ugovori ORDER BY id DESC LIMIT 1");
		$first_ugovor = $this->db->getOne("SELECT id FROM ugovori ORDER BY id ASC LIMIT 1");
		
		$this->assign("first_ugovor",$first_ugovor);
		$this->assign("prev_ugovor",$prev_ugovor);
		$this->assign("next_ugovor",$next_ugovor);
		$this->assign("last_ugovor",$last_ugovor);
		
		// fill variables from query resource
		foreach ($ugovor as $k=>$v){
			$$k=$v;
		}
		$iznos_popust = $this->getUgovorPopust($id);
		
		$this->assign("ugovor_id",$this->id);
		$this->assign("br_ugovora",$broj);
		$this->assign("ugovor_komitent_id",$komitent);
		$this->assign("ugovor_komitent_name",$this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=$komitent LIMIT 1"));
		$this->assign("ugovor_komitent_zr",$komitent_ziro);
		$this->assign("ugovor_komitent_banka",$komitent_banka);
		$this->assign("ugovor_datum",datetime2str($datum));
		
		$q = "SELECT * FROM ugovori_plan_otplate WHERE ugovor_id=$this->id ORDER BY plan_id DESC LIMIT 1";
		//echo $q;
		list($plan) = $this->db->select2($q);
		
		$unixdatum = date2int(datetime2str($datum));
		list($cenovnik_id, $cenovnik_datum) = $this->db->getRow("SELECT cenovnik_id, cenovnik_start FROM cenovnici WHERE cenovnik_start<$unixdatum ORDER BY cenovnik_start DESC LIMIT 1");
		if ($cenovnik_id===null) {
			list($cenovnik_id,$cenovnik_datum) = $this->db->getRow("SELECT cenovnik_id, cenovnik_start FROM cenovnici ORDER BY cenovnik_id LIMIT 1");
		}
		if($cenovnik_id>0){
			$this->assign("cenovnik_id",$cenovnik_id);
			$this->assign("cenovnik_datum",int2date($cenovnik_datum));
		}
			
		$polja = $this->get_UgovorTipovi($tip_ugovora);
			$this->assign("ugovor_tip_id",$polja["polje_id"]);
			$this->assign("ugovor_tip",$polja["polje_ime"]);
			$this->assign("ugovor_tip_selected",$polja["polje_selected"]);
		$polja = $this->get_UgovorProblemi($problem);
			$this->assign("problem_id",$polja["polje_id"]);
			$this->assign("problem",$polja["polje_ime"]);
			$this->assign("problem_selected",$polja["polje_selected"]);
		$this->assign("ugovor_lid",$lid);
		$lok_tip=$this->db->getOne("SELECT lok_tip FROM lokacije WHERE lid=$lid LIMIT 1");
		$lok_grad_display = $lok_ops_display = $lok_nas_display = "none";
		switch ($lok_tip){
			case "1":
				$lok_grad_display="block";
				break;
			case "2":
				$lok_ops_display="block";
				break;
			case "3":
				$lok_nas_display="block";
				break;
		}
		$lok_grad_display = ($lok_ops_display=="none" && $lok_nas_display=="none")?"block":$lok_grad_display;
		$this->assign("lok_grad_display",$lok_grad_display);
		$this->assign("lok_ops_display",$lok_ops_display);
		$this->assign("lok_nas_display",$lok_nas_display);
		$polja = $this->get_LokacijeTipovi($lok_tip);
			$this->assign("lok_tip_id",$polja["polje_id"]);
			$this->assign("lok_tip_name",$polja["polje_ime"]);
			$this->assign("lok_tip_selected",$polja["polje_selected"]);
		$polja = $this->get_Lokacije(1,$lid);
			$this->assign("lok_grad_id",$polja["polje_id"]);
			$this->assign("lok_grad_name",$polja["polje_ime"]);
			$this->assign("lok_grad_ptt",$polja["polje_ptt"]);
			$this->assign("lok_grad_selected",$polja["polje_selected"]);
		$polja = $this->get_Lokacije(2,$lid);
			$this->assign("lok_ops_id",$polja["polje_id"]);
			$this->assign("lok_ops_name",$polja["polje_ime"]);
			$this->assign("lok_ops_ptt",$polja["polje_ptt"]);
			$this->assign("lok_ops_selected",$polja["polje_selected"]);
		$polja = $this->get_Lokacije(3,$lid);
			$this->assign("lok_nas_id",$polja["polje_id"]);
			$this->assign("lok_nas_name",$polja["polje_ime"]);
			$this->assign("lok_nas_ptt",$polja["polje_ptt"]);
			$this->assign("lok_nas_selected",$polja["polje_selected"]);
		$polja = $this->get_Radnici($saradnik);
			$this->assign("ugovor_radnik_id",$polja["polje_id"]);
			$this->assign("ugovor_radnik_ime",$polja["polje_ime"]);
			$this->assign("ugovor_radnik_prezime",$polja["polje_prezime"]);
			$this->assign("ugovor_radnik_selected",$polja["polje_selected"]);
		$polja = $this->get_Porezi($porez);
			$this->assign("porez_id",$polja["polje_id"]);
			$this->assign("porez_name",$polja["polje_ime"]);
			$this->assign("porez_perc",$polja["polje_perc"]);
			$this->assign("porez_selected",$polja["polje_selected"]);
		$polja = $this->get_StatusiIzrade($izrada_status);
			$this->assign("ugovor_izrada_status_id",$polja["polje_id"]);
			$this->assign("ugovor_izrada_status",$polja["polje_ime"]);
			$this->assign("ugovor_izrada_status_selected",$polja["polje_selected"]);
		if ($izrada_status>1) {
			$this->assign("display_izrada_status_extend","block");
		}else{
			$this->assign("display_izrada_status_extend","none");
		}
		
		/*********************	OTPLATE	****************************/
		$otplata = $this->get_Otplate();
		$otplaceno = 0;
		if ($otplata !==false) {
			foreach ($otplata as $otpl){
				$otplaceno += floatval($otpl["iznos"]);
			}
		}
		//$otplaceno += floatval($plan["avans_bruto"]);
		$ugovor_dug = floatval($ugovor["iznos_bruto"])-$otplaceno;
		$this->assign("ugovor_iznos_uplaceno",number_format($otplaceno,2,",","."));
		$this->assign("ugovor_iznos_dug",number_format($ugovor_dug,2,",","."));
		
		/*********************	DOPISI	****************************/
		
		$dopisi = $this->get_Dopisi();
		$dopisTip = $this->get_DopisTipovi();
		$dopisRadnici = $this->get_Radnici();
		$dopisIshodi = $this->get_DopisIshodi();
		
		// template
		$this->assign("dt_id",$dopisTip["polje_id"]);
		$this->assign("dt_name",$dopisTip["polje_ime"]);
		$this->assign("dt_selected",$dopisTip["polje_selected"]);
		$this->assign("radnik_id",$dopisRadnici["polje_id"]);
		$this->assign("radnik_ime",$dopisRadnici["polje_ime"]);
		$this->assign("radnik_prezime",$dopisRadnici["polje_prezime"]);
		$this->assign("di_id",$dopisIshodi["polje_id"]);
		$this->assign("di_name",$dopisIshodi["polje_ime"]);
		$this->assign("dopis_template",$this->parseTemplate("ugovori/novi/dopis.tpl"));
		
		if($dopisi !== false){
			foreach ($dopisi as $k=>$v){
				$dopis_num[$k] = $k+1;
				$this->assign("dopis_index",$k+1);
				
				$this->selectOption($dopisTip,$v["tip"]);
				$this->assign("dt_id",$dopisTip["polje_id"]);
				$this->assign("dt_name",$dopisTip["polje_ime"]);
				$this->assign("dt_selected",$dopisTip["polje_selected"]);
				
				$this->assign("d_date",date("d.m.Y",$v["time"]));
				
				
				$this->selectOption($dopisRadnici,$v["radnik"]);
				$this->assign("radnik_id",$dopisRadnici["polje_id"]);
				$this->assign("radnik_ime",$dopisRadnici["polje_ime"]);
				$this->assign("radnik_prezime",$dopisRadnici["polje_prezime"]);
				$this->assign("radnik_selected",$dopisRadnici["polje_selected"]);
				
				$this->assign("dopis_radnik_date",int2date($v["radnik_time"]));
				
				$this->selectOption($dopisIshodi,$v["ishod"]);
				$this->assign("di_id",$dopisIshodi["polje_id"]);
				$this->assign("di_name",$dopisIshodi["polje_ime"]);
				$this->assign("di_selected",$dopisIshodi["polje_selected"]);
				
				$this->assign("dopis_ishod_date",int2date($v["ishod_time"]));
				$this->assign("dopis_napomena",$v["napomena"]);
				
				$dopis_html[$k] = $this->parseTemplate("ugovori/view/dopis.tpl");
			}
			$this->assign("dopis_num",$dopis_num);
			$this->assign("dopis_html",$dopis_html);
			$this->assign("dopis_maxz",count($dopisi));
		}
				
		
		/*********************	DEKLARACIJE	****************************/
		
		$deklaracije = $this->get_Deklaracije();
		$deklTip = $this->get_DeklaracijaTipPoruke();
		
		// template
		$this->assign("dmt_id",$deklTip["polje_id"]);
		$this->assign("dmt_name",$deklTip["polje_ime"]);
		$this->assign("deklaracija_template",$this->parseTemplate("ugovori/novi/deklaracija.tpl"));
		if($deklaracije !== false){
			foreach ($deklaracije as $k=>$v){
				$dekl_num[$k] = $k+1;
				$this->assign("dekl_index",$k+1);
				$this->selectOption($deklTip,$v["tip"]);
				$this->assign("dmt_id",$deklTip["polje_id"]);
				$this->assign("dmt_name",$deklTip["polje_ime"]);
				$this->assign("dmt_selected",$deklTip["polje_selected"]);
				
				foreach ($v as $dekl_key=>$dekl_val){
					$this->assign("dekl_".$dekl_key,$dekl_val);
				}
				$this->assign("dekl_datum",int2date($v["datum"]));
				
				$dekl_html[$k] = $this->parseTemplate("ugovori/view/deklaracija.tpl");
				
			}
			$this->assign("dekl_num",$dekl_num);
			$this->assign("dekl_html",$dekl_html);
			$this->assign("dekl_maxz",count($deklaracije));
		}
		
		
		/*********************	STAVKE	****************************/
		$q = "SELECT * FROM ugovori_stavke WHERE ugovor_id=$this->id ORDER BY stavka_id";
		$stavke = $this->db->select2($q);
		
		$cene = $this->get_Cene($cenovnik_id);	//print_rarray(array_keys($cene));
		$paketi = $this->get_Paketi($cenovnik_id);
		$delatnosti = $this->get_Delatnosti();
		$kampanje = $this->get_Kampanje();
		
		if ($stavke !== false) {
			foreach ($stavke as $k=>$stavka){
				$stavka_num[$k] = ($k+1);
				if($stavka["paket"]>0){
					$this->selectOption($paketi,$stavka["artikl"]);
					$this->selectOption($cene,"");
				}else{
					$this->selectOption($paketi,"");
					$this->selectOption($cene,$stavka["artikl"]);
				}
				
				$cene_o = "";
				if(count($cene["polje_id"])>0){
					foreach ($cene["polje_id"] as $key_cene=>$val){
						$cene_o .= "\n\t".'<option paket="0" '
								.'value="'.$val.'" '
								.'artikl="'.$cene["polje_artikl"][$key_cene].'" '
								.'tip="'.$cene["polje_artikl_tip"][$key_cene].'" '
								.'boja="'.$cene["polje_artikl_boja"][$key_cene].'" '
								.'mera="'.$cene["polje_artikl_mera"][$key_cene].'" '
								.'jmere="'.$cene["polje_artikl_jmere"][$key_cene].'" '
								.'cena="'.$cene["polje_cena"][$key_cene].'"';
						if (strlen($cene["polje_selected"][$key_cene])) {
							$cene_o .= " selected";
						}
						$cene_o .= '>'
								.$cene["polje_ime"][$key_cene]
								.'</option>';
					}
				}
				$cene_options[$k] = $cene_o;
				
				$paketi_o = "";
				//print_rarray($paketi);
				if(count($paketi["polje_id"])>0){
					foreach ($paketi["polje_id"] as $key_paket=>$val){
						$paketi_o .= "\n\t".'<option paket="1" '
								.'value="'.$val.'" '
								.'sadrzaj="'.$paketi["polje_sadrzaj"][$key_paket].'" '
								.'sadrzaj_desc="'.$paketi["polje_sadrzaj_desc"][$key_paket].'" '
								.'boja="'.$paketi["polje_artikl_boja"][$key_paket].'" '
								.'mera="'.$paketi["polje_artikl_mera"][$key_paket].'" '
								.'jmere="'.$cene["polje_artikl_jmere"][$key_cene].'" '
								.'cena="'.$paketi["polje_cena"][$key_paket].'"';
						if (strlen($paketi["polje_selected"][$key_paket])) {
							$paketi_o .= " selected";
						}
						$paketi_o .= '>'
								.$paketi["polje_ime"][$key_paket]." &middot; ".date("d.m.Y",$paketi["polje_start"][$key_paket])."-".date("d.m.Y",$paketi["polje_end"][$key_paket])
								.'</option>';
					}
				}
				$paketi_options[$k] = $paketi_o;
				
				$stavka_kol[$k] = $stavka["kolicina"];
				
				$tmp = $stavka["kolicina"]>0?bcdiv($stavka["iznos_neto"],$stavka["kolicina"],2):0;
				$stavka_iznos_neto[$k] = number_format($tmp,2,",",".");
				$stavka_popust[$k] = number_format($stavka["popust"],2,",",".");
				$stavka_porez[$k] = number_format($stavka["porez"],2,",",".");
				$stavka_iznos_bruto[$k] = number_format($stavka["iznos_bruto"],2,",",".");
				
				$stavka_komitent_id[$k] = $stavka["komitent"];
				$stavka_komitent_name[$k] = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$stavka["komitent"]." LIMIT 1");
				
				$this->selectOption($delatnosti,$stavka["delatnost"]);
				$del_o = "";
				foreach ($delatnosti["polje_id"] as $key_del=>$v){
					$del_o .= "\n\t".'<option '
						   .'value="'.$v.'" '
						   .'title="'.$v.'"';
					if (strlen(trim($delatnosti["polje_selected"][$key_del]))) {
						$del_o .= " selected";
					}
					$del_o .= '>'.$delatnosti["polje_ime"][$key_del]
						   .'</option>';
				}
				$delatnosti_options[$k] = $del_o;
				
				$this->selectOption($kampanje,$stavka["kampanja"]);
				$kamp_o = "";
				foreach ($kampanje["polje_id"] as $key_kam=>$v){
					$kamp_o .= "\n\t".'<option '
							.'value="'.$v.'" '
							.'title="['.$v.'] '.int2date($kampanje["polje_start"][$key_kam]).' - '.int2date($kampanje["polje_end"][$key_kam]).'" ';
					if (strlen($kampanje["polje_selected"][$key_kam])) {
						$kamp_o .= " selected";
					}
					$kamp_o .= '>'.$kampanje["polje_ime"][$key_kam]
							.'</option>';
				}
				$kampanje_options[$k] = $kamp_o;
				
				
				$tok_paket[$k] = ($stavka["paket"]>0)?"1":"";
				$q = "SELECT * FROM ugovori_tok_realizacije WHERE stavka=".$stavka["stavka_id"];
				
/* single */	if ($stavka["paket"]==0) {
					$itdt_tok_id[$k] = ($k+1);
					list($toks) = $this->db->select2($q);
					$q = "SELECT art_name, arttip_name, art_boja_code, cena_art_mera, unit_svalue "
						."FROM cene c, artikli a, artikli_tipovi at, artikli_boje ab, jedinice_mere jm "
						."WHERE c.cena_art_id=a.art_id "
						."AND c.cena_art_tip=at.arttip_id "
						."AND c.cena_art_boja=ab.art_boja_id "
						."AND c.cena_art_jed_mere=jm.unit_id "
						."AND c.cena_id=".$stavka["artikl"]." "
						."LIMIT 1";
					list($artname, $arttip, $artboja, $artmera, $artjm) = $this->db->getRow($q);
					$stavka_head = $artname;
					if (strlen($arttip)) {$stavka_head .= " - ".$arttip;}
					if (strlen($artboja)) {$stavka_head .= " - ".$artboja;}
					if (strlen($artjm)) {$stavka_head .= " - ".$artmera." ".$artjm;}
					$this->assign("stavka_head_name",$stavka_head);
					$this->assign("stavka_tok_art_id",$stavka["artikl"]);
					
					$this->tokMaterijalAssign($toks["tok_id"]);
					
					if ($toks["materijal_date_ocek"]>0) {
						$this->assign("mat_ocekuje",int2date($toks["materijal_date_ocek"]));
					}else{
						$this->assign("mat_ocekuje","");
					}
					if ($toks["datum_poslato"]>0) {
						$this->assign("komitent_poslato",int2date($toks["datum_poslato"]));
					}else{
						$this->assign("komitent_poslato","");
					}
					if ($toks["datum_obavesten"]>0) {
						$this->assign("komitent_obavesten",int2date($toks["datum_obavesten"]));
					}else{
						$this->assign("komitent_obavesten","");
					}
					if ($toks["datum_potvrda"]>0) {
						$this->assign("komitent_potvrda",int2date($toks["datum_potvrda"]));
					}else{
						$this->assign("komitent_potvrda","");
					}
					if ($toks["obnova"]>0) {
						$this->assign("komitent_obnova"," checked");
					}else{
						$this->assign("komitent_obnova","");
					}
					if (strlen(trim($toks["spec_zah"]))>0) {
						$this->assign("ugovor_tok_speczah",$toks["spec_zah"]);
					}else{
						$this->assign("ugovor_tok_speczah","specijalni zahtevi...");
					}
					if (strlen(trim($toks["napomena"]))>0) {
						$this->assign("ugovor_tok_nap",$toks["napomena"]);
					}else{
						$this->assign("ugovor_tok_nap","napomena...");
					}
					
					$this->tokActionsAssign($toks["tok_id"]);
					$this->intenetDataAssign($toks["tok_id"],$itdt_tok_id[$k]);
					$this->bookDataAssign($toks["tok_id"],$itdt_tok_id[$k],$delatnosti);
					
					$tok_data[$k] = $this->parseTemplate("ugovori/view/stavka_tok_item.tpl");
/* package */	}else{
					$toks = $this->db->select2($q);
					$tokpak = "";
					foreach ($toks as $key_tok=>$t){
						$itdt_tok_id[$k] = ($k+1)."_".$key_tok;
						$q = "SELECT art_name, arttip_name, art_boja_code, cena_art_mera, unit_svalue "
							."FROM cene c, artikli a, artikli_tipovi at, artikli_boje ab, jedinice_mere jm "
							."WHERE c.cena_art_id=a.art_id "
							."AND c.cena_art_tip=at.arttip_id "
							."AND c.cena_art_boja=ab.art_boja_id "
							."AND c.cena_art_jed_mere=jm.unit_id "
							."AND c.cena_id=".$t["artikl"]." "
							."LIMIT 1";
						list($artname, $arttip, $artboja, $artmera, $artjm) = $this->db->getRow($q);
						$stavka_head = $artname;
						if (strlen($arttip)) {$stavka_head .= " / ".$arttip;}
						if (strlen($artboja)) {$stavka_head .= " / ".$artboja;}
						if (strlen($artjm)) {$stavka_head .= " / ".$artmera." ".$artjm;}
						
						$this->assign("stavka_head_name",$stavka_head);
						$this->assign("stavka_tok_art_id",$t["artikl"]);
						
						$this->tokMaterijalAssign($t["tok_id"]);
						
						if ($t["materijal_date_ocek"]) {
							$this->assign("mat_ocekuje",int2date($t["materijal_date_ocek"]));
						}else{
							$this->assign("mat_ocekuje","");
						}
						if ($t["datum_poslato"]) {
							$this->assign("komitent_poslato",int2date($t["datum_poslato"]));
						}else{
							$this->assign("komitent_poslato","");
						}
						if ($t["datum_obavesten"]) {
							$this->assign("komitent_obavesten",int2date($t["datum_obavesten"]));
						}else{
							$this->assign("komitent_obavesten","");
						}
						if ($t["datum_potvrda"]) {
							$this->assign("komitent_potvrda",int2date($t["datum_potvrda"]));
						}else{
							$this->assign("komitent_potvrda","");
						}
						if ($t["obnova"]>0) {
							$this->assign("komitent_obnova"," checked");
						}else{
							$this->assign("komitent_obnova","");
						}
						
						if (strlen(trim($t["spec_zah"]))>0) {
							$this->assign("ugovor_tok_speczah",$t["spec_zah"]);
						}else{
							$this->assign("ugovor_tok_speczah","specijalni zahtevi...");
						}
						if (strlen(trim($t["napomena"]))>0) {
							$this->assign("ugovor_tok_nap",$t["napomena"]);
						}else{
							$this->assign("ugovor_tok_nap","napomena...");
						}
						
						$this->tokActionsAssign($t["tok_id"]);
						$this->intenetDataAssign($t["tok_id"],$itdt_tok_id[$k]);
						$this->bookDataAssign($t["tok_id"],$itdt_tok_id[$k],$delatnosti);
						
						$tokpak .= '<li style="clear:both" id="item_data_'.($k+1).'_'.$key_tok.'">'.$this->parseTemplate("ugovori/view/stavka_tok_item.tpl").'<br clear="both" /></li>';
					}
					$tok_data[$k] = "<ul style=\"list-style-type:none;\">$tokpak</ul>";
				}
			}
			
			$this->assign("ugovor_obnova_popust",$obnova_popust);
			$this->assign("ugovor_ne_obnova",($ne_obnova>0?" checked":""));
			$this->assign("ugovor_iznos_neto",number_format($iznos_neto,2,",","."));
			$this->assign("ugovor_iznos_korekcija",number_format($iznos_popust,2,",","."));
			$this->assign("ugovor_iznos_porez",number_format($iznos_porez,2,",","."));
			$this->assign("ugovor_iznos_bruto",number_format($iznos_bruto,2,",","."));
			
			$this->assign("ugovor_napomena",$napomena);
			
			//print_rarray($tok_data);
			$this->assign("stavka_num",$stavka_num);
			$this->assign("stavka_artikli_options",$cene_options);
			$this->assign("stavka_paketi_options",$paketi_options);
			$this->assign("stavka_kol",$stavka_kol);
			$this->assign("stavka_iznos_neto",$stavka_iznos_neto);
			$this->assign("stavka_popust",$stavka_popust);
			$this->assign("stavka_porez",$stavka_porez);
			$this->assign("stavka_iznos_bruto",$stavka_iznos_bruto);
			$this->assign("stavka_komitent_id",$stavka_komitent_id);
			$this->assign("stavka_komitent_name",$stavka_komitent_name);
			$this->assign("stavka_delatnost_options",$delatnosti_options);
			$this->assign("stavka_kampanja_options",$kampanje_options);
			$this->assign("itdt_paket",$tok_paket);
			$this->assign("itdt_tok",$tok_data);
			//file_put_contents("tmpVOJA",implode("\r\n\r\n\r\n*****************************************\r\n\r\n\r\n",$tok_data));
		}
		
		/******************************* OTPLATA ****************************/
		$q = "SELECT * FROM ugovori_otplate WHERE ugoid=$this->id ORDER BY datum";
		$otplata = $this->db->select2($q);
		if ($otplata !== false) {
			$sum_bruto = $sum_neto = $sum_porez = 0;
			foreach ($otplata as $k=>$row){
				$nums[$k] = $k+1;
				$ids[$k] = $row["id"];
				$izvodi[$k] = $row["izvod"];
				$dates[$k] = int2date($row["datum"]);
				$brutos[$k] = number_format($row["iznos"],2,",",".");
				$netos[$k] = number_format($row["iznosbp"],2,",",".");
				$porezs[$k] = number_format($row["iznosporez"],2,",",".");
				$kamate[$k] = $row["kamata"]>0?number_format($row["kamata"],2,",","."):"";
				
				$sum_bruto	+= $row["iznos"];
				$sum_neto	+= $row["iznosbp"];
				$sum_porez	+= $row["iznosporez"];
			}
			$this->assign("otp_num",$nums);
			$this->assign("otp_id",$ids);
			$this->assign("otp_izvod",$izvodi);
			$this->assign("otp_date",$dates);
			$this->assign("otp_bruto",$brutos);
			$this->assign("otp_neto",$netos);
			$this->assign("otp_porez",$porezs);
			$this->assign("otp_kamata",$kamate);
			$this->assign("otp_sum_bruto",number_format($sum_bruto,2,",","."));
			$this->assign("otp_sum_neto",number_format($sum_neto,2,",","."));
			$this->assign("otp_sum_porez",number_format($sum_porez,2,",","."));
		}
		
		/******************************* PLAN OTPLATE ****************************/
		$otplacene_rate = $sum_bruto;
		
		$polja = $this->get_UgovorNaciniPlacanja($plan["nacin_placanja"]);//print_rarray($polja);
			$this->assign("np_id",$polja["polje_id"]);
			$this->assign("np_name",$polja["polje_ime"]);
			$this->assign("np_code",$polja["polje_code"]);
			$this->assign("np_selected",$polja["polje_selected"]);
		if ($plan) {
			$brrata = $plan["nacin_placanja"]==2?0:$plan["broj_rata"];
			$this->assign("plan_br_rata",sprintf("%s",$brrata));
			$this->assign("ugovor_avans",number_format($plan["avans_bruto"],2,",","."));
			
			if ($plan["avans_bruto"]>0) {
				$otplacene_rate -= $plan["avans_bruto"];
				$this->assign("avans_neto",number_format($plan["avans_neto"],2,",","."));
				$this->assign("avans_bruto",number_format($plan["avans_bruto"],2,",","."));
				$this->assign("avans_porez",number_format($plan["avans_porez"],2,",","."));
			}
			$plan_date = datetime2str($plan["avans_date"]);
			$this->assign("avans_date",$plan_date);
			
			$sum_neto = $plan["avans_neto"];
			$sum_bruto = $plan["avans_bruto"];
			$sum_porez = $plan["avans_porez"];
			
			$rate = $this->db->select2("SELECT * FROM ugovori_plan_rate WHERE rata_plan=".$plan["plan_id"]." ORDER BY datum ASC");
			if ($rate !== false && $brrata>0) {
				foreach ($rate as $k=>$rata){
					
					$otplacene_rate -= $rata["bruto"];
					
					$key = count($plan_rata_num);
					$plan_rata_num[$key] = $k+1;
					$plan_rata_id[$key] = $rata["rata_id"];
					$plan_rata_neto[$key] = number_format($rata["neto"],2,",",".");
					$plan_rata_porez[$key] = number_format($rata["porez"],2,",",".");
					$plan_rata_bruto[$key] = number_format($rata["bruto"],2,",",".");
					$plan_rata_kamata[$key] = number_format($rata["kamata"],2,",",".");
					$plan_rata_tros[$key] = number_format($rata["troskovi"],2,",",".");
					$plan_rata_date[$key] = int2date($rata["datum"]);
					
					//$plan_rata_otpl[$key] = $rata["otplaceno"]>0?" checked":"";
					$plan_rata_otpl[$key] = $otplacene_rate>=0?" checked":"";
					
					$sum_neto += $rata["neto"];
					$sum_bruto += $rata["bruto"];
					$sum_porez += $rata["porez"];
					
				}
				$this->assign("num",$plan_rata_num);
				$this->assign("plan_rata_id",$plan_rata_id);
				$this->assign("plan_neto",$plan_rata_neto);
				$this->assign("plan_bruto",$plan_rata_bruto);
				$this->assign("plan_porez",$plan_rata_porez);
				$this->assign("plan_kamata",$plan_rata_kamata);
				$this->assign("plan_tros",$plan_rata_tros);
				$this->assign("datum",$plan_rata_date);
				$this->assign("plan_otplata",$plan_rata_otpl);
			}
			
			$this->assign("ukupno_neto",number_format($sum_neto,2,",","."));
			$this->assign("ukupno_bruto",number_format($sum_bruto,2,",","."));
			$this->assign("ukupno_porez",number_format($sum_porez,2,",","."));
			
			$this->assign("plan_table",$this->parseTemplate("ugovori/plan_otplate_table.tpl"));
		}
		
		$this->assign("searchForm",$this->parseTemplate("ugovori/pretraga_forma.tpl"));
		
		// throwing the HTML
		$this->assign("basic_data",$this->parseTemplate("ugovori/view/basic_data.tpl"));
		$this->assign("items",$this->parseTemplate("ugovori/view/stavke.tpl"));
		$this->assign("payment",$this->parseTemplate("ugovori/view/payment.tpl"));
		echo $this->parseTemplate("ugovori/view/view.tpl");
	}
	
	function tokMaterijalAssign($tokid){
		$q = "SELECT * FROM tok_materijali WHERE tok=".$tokid." ORDER BY date_ulaz DESC";
		$tokmat = $this->db->select2($q);
		if ($tokmat !== false) {
			$tokmat_id = $tokmat_datum = $tokmat_upotrebljiv = $tokmat_mat_names = $tokmat_ulaz_name = $tokmat_napomena = array();
			foreach ($tokmat as $key_tokmat=>$tm){
				$tokmat_id[$key_tokmat] = $tm["id"];
				$ulazname = $this->db->getOne("SELECT tip_ulaz_name FROM materijal_tip_ulaza WHERE tip_ulaz_id=".$tm["tip_ulaz"]);
				$tokmat_ulaz_name[$key_tokmat] = $ulazname;
				$tokmat_ulaz_id[$key_tokmat] = $tm["tip_ulaz"];
				if(strlen($tm["tip_materijal"])){
					$mats = explode(";",$tm["tip_materijal"]);
					$mats_txt="";
					foreach ($mats as $matid){
						$mats_txt .= ", ".$this->db->getOne("SELECT mattip_name FROM materijal_tipovi WHERE mattip_id=$matid LIMIT 1");
					}
					$tokmat_mat_names[$key_tokmat] = substr($mats_txt,2);
					$tokmat_mat_ids[$key_tokmat] = $tm["tip_materijal"];
				}
				$tokmat_upotrebljiv[$key_tokmat] = $tm["upotrebljiv"]>0?"yes":"";
				$tokmat_datum[$key_tokmat] = date("d.m.Y",$tm["date_ulaz"]);
				$tokmat_napomena[$key_tokmat] = $tm["napomena"];
				
				$tokmat_data[$key_tokmat] = "id=".$tm["id"]."{!!}"
										   ."date=".date("d.m.Y",$tm["date_ulaz"])."{!!}"
										   ."tip_ulaza=".$tm["tip_ulaz"]."{!!}"
										   ."upotrebljiv=".$tm["upotrebljiv"]."{!!}"
										   ."napomena=".urlencode($tm["napomena"])."{!!}"
										   ."tip_materijala=".str_replace(";","{!}",$tm["tip_materijal"]);
			}
			$this->assign("mat_data",$tokmat_data);
			$this->assign("mat_id",$tokmat_id);
			$this->assign("mat_usao",$tokmat_datum);
			$this->assign("mat_upotrebljiv",$tokmat_upotrebljiv);
			$this->assign("mat_vrste",$tokmat_mat_names);
			$this->assign("mat_tip_ulaza",$tokmat_ulaz_name);
			$this->assign("mat_napomena",$tokmat_napomena);
		}else{
			$this->assign("mat_id",array());
		}
	}
	
	function tokActionsAssign($tokid){
		$q = "SELECT * FROM ugovori_tok_actions WHERE tok=$tokid ORDER BY date_uzimanja DESC";
		$res = $this->db->select2($q);
		if ($res !== false) {
			$len = count($res);
			foreach ($res as $k=>$row){
				$act_num[$k] = ($len-$k);
				$act_id[$k] = $row["action_id"];
				$act_editor_name[$k] = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$row["urednik"]." LIMIT 1");
				$act_designer_name[$k] = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$row["designer"]." LIMIT 1");
				
				$act_done[$k] = $row["done"]>0?"border:2px solid darkgreen;":"";
				$act_designer_note[$k] = $row["designer_note"];
				$act_date_out[$k] = ($row["date_uzimanja"]>0)?date("d.m.Y",$row["date_uzimanja"]):"";
				$act_date_in[$k] = ($row["date_vracanja"]>0)?date("d.m.Y",$row["date_vracanja"]):"";
				$act_napomena[$k] = $row["napomena"];
				
				if (strlen(trim($row["napomena"]))) {
					$comm_start[$k] = $comm_end[$k] = "";
				}else{
					$comm_start[$k] = "<!--";
					$comm_end[$k] = "-->";
				}
				
				$act_data[$k] = "id=".$row["action_id"]."{!!}"
								."editor=".$row["urednik"]."{!!}"
								."designer=".$row["designer"]."{!!}"
								."done=".$row["done"]."{!!}"
								."date_out=".date("d.m.Y",$row["date_uzimanja"])."{!!}"
								."date_in=".date("d.m.Y",$row["date_vracanja"])."{!!}"
								."designer_note=".urlencode($row["designer_note"])."{!!}"
								."napomena=".urlencode($row["napomena"]);
			}
			$this->assign("act_num",$act_num);
			$this->assign("act_id",$act_id);
			$this->assign("act_editor",$act_editor_name);
			$this->assign("napomena",$act_napomena);
			$this->assign("imanap_start",$comm_start);
			$this->assign("imanap_end",$comm_end);
			$this->assign("act_out",$act_date_out);
			$this->assign("act_in",$act_date_in);
			$this->assign("act_designer",$act_designer_name);
			$this->assign("act_done",$act_done);
			$this->assign("act_designer_note",$act_designer_note);
			$this->assign("act_data",$act_data);
		}else{
			$this->assign("act_num",array());
		}
	}
	
	function intenetDataAssign($tokid,$itdt_id){
		$this->assign("itdt_id",$itdt_id);
		// PERIODI TRAJANJA
		$periodi = $this->db->select2("SELECT * FROM tok_trajanje_periodi WHERE id_tok=$tokid ORDER BY datum_start");
		if ($periodi!==false) {
			foreach ($periodi as $k=>$per){
				$period_num[$k] = sprintf("%s",$k);
				$period_start[$k] = int2date($per["datum_start"]);
				$period_end[$k] = int2date($per["datum_end"]);
			}
		}else{
			$period_num = array("0");
		}
		$this->assign("period_num",$period_num);
		$this->assign("period_start",$period_start);
		$this->assign("period_end",$period_end);
		
		// INTERNET DATA
		$internet = $this->db->select2("SELECT * FROM internet_data WHERE id_tok=$tokid LIMIT 1");
		if ($internet !== false) {
			$internet = $internet[0];
			//echo "$tokid - ".$internet["profil_tip"]."<br />";
			
			
			// kwords
			$kwords = trim($internet["kwords"]);
			$this->assign("kwords",$kwords);
			
			// profile
			$polja = $this->get_ProfiliTipovi($internet["profil_tip"]);
				$this->assign("profil_tip_id",$polja["polje_id"]);
				$this->assign("profil_tip_name",$polja["polje_ime"]);
				$this->assign("profil_tip_selected",$polja["selected"]);
			$this->assign("profile_start",int2date($internet["profil_start"]));
			$this->assign("profile_end",int2date($internet["profil_end"]));
			$this->assign("logo_pre",$this->logoDirectory);
			$this->assign("profile_logo",$internet["profil_logo"]);
			$this->assign("profile_srp",$internet["profil_srp"]);
			$this->assign("profile_eng",$internet["profil_eng"]);
			
			// banner
			$polja = $this->get_BaneriTipovi($internet["baner_tip"]);
				$this->assign("baner_tip_id",$polja["polje_id"]);
				$this->assign("baner_tip_name",$polja["polje_ime"]);
				$this->assign("baner_tip_selected",$polja["polje_selected"]);
			$this->assign("baner_start",int2date($internet["baner_start"]));
			$this->assign("baner_end",int2date($internet["baner_end"]));
			$this->assign("baner_pre","/baner/");
			$this->assign("baner_name",$internet["baner_name"]);
			
			// www
			$this->assign("www_test",$internet["www_test"]);
			$this->assign("www_final",$internet["www_final"]);
			
			// hosting YP
			$this->assign("host_yp_start",int2date($internet["hosting_yp_start"]));
			$this->assign("host_yp_end",int2date($internet["hosting_yp_end"]));
			
			// hosting
			$polja = $this->get_HostingProviders($internet["hosting_provider"]);
				$this->assign("provider_id",$polja["polje_id"]);
				$this->assign("provider_name",$polja["polje_ime"]);
				$this->assign("provider_selected",$polja["polje_selected"]);
			$this->assign("host_start",int2date($internet["hosting_start"]));
			$this->assign("host_end",int2date($internet["hosting_end"]));
			
			// domen
			$this->assign("domen_name",$internet["domain_name"]);
			$this->assign("domen_start",int2date($internet["domain_start"]));
			$this->assign("domen_end",int2date($internet["domain_end"]));
		}
	}
	
	function bookDataAssign($tokid,$itdt_id,$delatnosti){
		$book = $this->db->select2("SELECT * FROM knjiga_data WHERE id_tok=$tokid LIMIT 1");
		$polja = $this->get_Delatnosti();
			$this->assign("link_del_id",$polja["polje_id"]);
			$this->assign("link_del_name",$polja["polje_ime"]);
			$this->assign("del_id",$polja["polje_id"]);
			$this->assign("del_name",$polja["polje_ime"]);
		if ($book !== false) {
			$book = $book[0];
			$this->selectOption($polja,$book["multi_start_del"]);
			$this->assign("del_selected",$polja["polje_selected"]);
			$this->assign("brands",$book["brands"]);
			
			$this->assign("multi_checked",$book["multi"]>0?" checked":"");
			$this->assign("multi_num_pages",sprintf("%s",$book["multi_num_pages"]));
			
			if (strlen(trim($book["links"]))) {
				$links = explode(";",$book["links"]);
				foreach ($links as $link_delid){
					$link_delid = intval($link_delid);
					if ($link_delid>0) {
						$options = "";
						foreach ($delatnosti["polje_id"] as $k=>$delid){
							$options .= '<option value="'.$delid.'" title="'.$delid.'"';
							if($delid == $link_delid){
								$options .= " selected";
							}
							$options .= '>'.$delatnosti["polje_ime"][$k].'</option>';
						}
						$links_options[] = $options;
					}
				}
				$this->assign("link_dels",$links_options);
			}else{
				$this->assign("link_dels",null);
			}
		}
		
	}
	
	function selectOption(&$object,$testVal,$keyname="polje_id",$keySelect="polje_selected"){
		if(count($object[$keyname])>0){
			foreach ($object[$keyname] as $key=>$val){
				if ($val == $testVal) {
					$object[$keySelect][$key] = " selected";
				}else{
					$object[$keySelect][$key] = "";
				}
			}
		}
	}
	
	function get_Uplate(){
		$res = $this->db->select2("SELECT * FROM ugovori_uplate WHERE ugovor_id=$this->id");
		return $res;
	}
	
	function get_Otplate(){
		return $this->db->select2("SELECT * FROM ugovori_otplate WHERE ugoid=$this->id");
	}
	
	function get_Dopisi(){
		$res = $this->db->select2("SELECT * FROM dopisi WHERE ugovor_id=$this->id ORDER BY time DESC");
		return $res;
	}
	
	function get_Deklaracije(){
		$res = $this->db->select2("SELECT * FROM deklaracije WHERE ugovor=$this->id ORDER BY datum DESC");
		return $res;
	}
}

?>